<script setup lang="ts">
import CustomNavbar from './components/CustomNavbar.vue'
import XtxSwiper from '@/components/XtxSwiper.vue'
import XtxGuess from '@/components/XtxGuess.vue'
import CategoryPanel from './components/CategoryPanel.vue'
import PageSkeleton from './components/PageSkeleton.vue'
import HotPanel from './components/HotPanel.vue'
import type { XtxGuessInstance } from '@/types/component'




import { getHomeBannerAPI, getHomeCategoryApi, getHomeHotAPI } from "@/services/home"
import type { BannerItem, CategoryItem, HotItem } from '@/types/home';
import { onMounted, ref, } from 'vue'
import { useGuessList } from '@/composables'
import { onLoad } from '@dcloudio/uni-app'
let list = ref<BannerItem[]>([])
const getHomeBannerData = async () => {
  const res = await getHomeBannerAPI()
  list.value = res.result
}

//分类
let cateList = ref<CategoryItem[]>([])
const getHomeCategoryData = async () => {
  const res = await getHomeCategoryApi()
  cateList.value = res.result
}


let hotList = ref<HotItem[]>([])
const getHomeHotData = async () => {
  const res = await getHomeHotAPI()
  hotList.value = res.result

}

console.log(hotList.value);


const {guessRef,onScrolltolower} = useGuessList()

//猜你喜欢的组件实例
// const guessRef = ref<XtxGuessInstance>()
// 滚动触底事件
// const onScrolltolower = () => {
//   console.log('触底啦');
  
//   guessRef.value?.getMore()
// }

// 下拉刷新状态
const isTriggered = ref(false)
// 自定义下拉刷新被触发
const onRefresherrefresh = async () => {

  // 开启动画
  isTriggered.value = true
  // 重置猜你喜欢组件数据
  guessRef.value?.resetData() // 加载数据
  await Promise.all([getHomeBannerData(), getHomeCategoryData(), getHomeHotData(),
    guessRef.value?.getMore()
  ]) // 关闭动画

  isTriggered.value = false
}

const isLoading = ref(false)
onLoad(async () => {
  isLoading.value = true
  await Promise.all([getHomeBannerData(), getHomeCategoryData(), getHomeHotData()])
  isLoading.value = false
})



</script>

<template>
  <CustomNavbar />
  <scroll-view scroll-y="true" @scrolltolower="onScrolltolower" refresher-enabled @refresherrefresh="onRefresherrefresh"
    :refresher-triggered="isTriggered" class="scroll-view">
    <PageSkeleton v-if="isLoading" />
    <template v-else>
      <XtxSwiper :list="list" />
      <CategoryPanel :cateList="cateList" />
      <HotPanel :hotList="hotList" />
      <XtxGuess ref="guessRef" />
    </template>
  </scroll-view>

</template>
  <style lang="scss">


    .scroll-view {
       height: 80vh;
    }
  </style>
